import {
  Controller,
  Post,
  Get,
  Body,
  Query,
  HttpCode,
  HttpStatus,
  ValidationPipe,
  Param,
} from '@nestjs/common';
import { RocketService } from './rocket.service';
import { Rocket } from '@/entity/rocket/rocket.entity';
import { Public } from '@/modules/auth/decorators/public.decorator';

@Controller('/api/rocket')
export class RocketController {
  constructor(private readonly RocketService: RocketService) {}

  @Post('insert')
  async insert(
    @Body() bodyParams: any,
  ): Promise<{ code: number; data: Array<Rocket>; message: string }> {
    try {
      const res = await this.RocketService.insert({...bodyParams, createAt: new Date() });
      return {
        code: res ? 200 : 1000,
        data: res ? res : [],
        message: res ? 'ok' : '',
      };
    } catch (error) {
      return {
        code: 500,
        data: [],
        message: error,
      };
    }
  }

  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('queryRocket')
  async queryCollect(
    @Query('publish_id', new ValidationPipe({ transform: true }))
    publish_id: string,
    @Query('userId', new ValidationPipe({ transform: true })) userId: string,
  ): Promise<{ code: number; data: Rocket[]; message: string }> {
    try {
      console.log('参数', publish_id);
      const res = await this.RocketService.findCollect(publish_id, userId);
      return {
        code: res ? 0 : 1000,
        data: res ? res : null,
        message: res ? 'ok' : res,
      };
      return res;
    } catch (error) {
      return {
        code: 500,
        data: [],
        message: error,
      };
    }
  }

  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('findAllRocket')
  async findAllCollect(): Promise<{
    code: number;
    data: Array<Rocket>;
    message: string;
  }> {
    try {
      const res = await this.RocketService.findAll();
      return {
        code: res ? 200 : 1000,
        data: res ? res : [],
        message: res ? 'ok' : '',
      };
    } catch (error) {
      return {
        code: 500,
        data: [],
        message: error,
      };
    }
  }

  @HttpCode(HttpStatus.OK)
  @Get('findUserRocket/:userId')
  async findUserRocket(
    @Param('userId') userId: string,
  ): Promise<{ code: number; data: Array<Rocket>; message: string }> {
    try {
      const rocketData = await this.RocketService.findCollectByUserId(userId);
      console.log('findUserRocket', rocketData);
      if (rocketData) {
        return {
          code: 200,
          data: rocketData,
          message: 'ok',
        };
      } else {
        return {
          code: 1000,
          data: null,
          message: 'error',
        };
      }
    } catch (error) {
      return {
        code: 500,
        data: null,
        message: error,
      };
    }
  }
}
